package visualization;

import graphs.XYInputsvOuputByThetaGraph;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import odor.Odor;

import main.InitializationException;
import structures.Network;

public class ThetaAnalysisFrame extends JFrame implements RunsSimulation
{
	private ArrayList<JNetwork> networkArray;
	private JTabbedPane networkProcesTabs;
	public static final int X_OFFSET = 105;
	public static final int Y_OFFSET = 55;
	private Odor[] battery;
	private ControlWidget myControlWidget;
	private XYInputsvOuputByThetaGraph outputGraph;
	
	public ThetaAnalysisFrame()
	{
		networkArray = new ArrayList<JNetwork>();
		networkProcesTabs = new JTabbedPane(JTabbedPane.LEFT);
		myControlWidget = new ControlWidget(this);
	}
	
	public ThetaAnalysisFrame(Network n)
	{
		networkArray = new ArrayList<JNetwork>();
		networkProcesTabs = new JTabbedPane(JTabbedPane.LEFT);
		
		addJNetworkTab(n);
		
		initializeGraphics();
		
		this.setContentPane(networkProcesTabs);
		this.validate();
		
		myControlWidget = new ControlWidget(this);
		myControlWidget.setVisible(true);
		
		this.setVisible(true);
	}
	
	public ThetaAnalysisFrame(Network n, Odor[] battery)
	{
		networkArray = new ArrayList<JNetwork>();
		networkProcesTabs = new JTabbedPane(JTabbedPane.LEFT);
		this.battery = battery;
		
		addJNetworkTab(n);
		
		initializeGraphics();
		
		this.setContentPane(networkProcesTabs);
		this.validate();
		
		myControlWidget = new ControlWidget(this, battery);
		myControlWidget.setVisible(true);
		
		this.setVisible(true);
	}
	
	public ThetaAnalysisFrame(int numNetworks, Odor[] battery)
	{
		networkArray = new ArrayList<JNetwork>();
		networkProcesTabs = new JTabbedPane(JTabbedPane.LEFT);
		this.battery = battery;
		
		for(int x=0; x<numNetworks; x++)
		{
			//Main.UNIVERSAL_TO_MITRAL_WEIGHT = (new java.util.Random()).nextInt((int)Math.round(Network.DEFAULT_MITRAL_TO_COL_CONNECTIVITY*Main.NUM_COLS)) +1;
			
			Network n = new Network(main.Main.NUM_COLS);
			
			try
			{
				n.initialize(null);
			}
			catch(InitializationException e)
			{
				e.printStackTrace();
				System.exit(0);
			}
			
			addJNetworkTab(n);
		}
		
		initializeGraphics();
		
		this.setContentPane(networkProcesTabs);
		this.validate();
		
		myControlWidget = new ControlWidget(this, battery);
		myControlWidget.setVisible(true);
		
		this.setVisible(true);
	}
	
	public void setOdor(Odor input)
	{
		for(JNetwork network : networkArray)
		{
			network.setOdor(input);
		}
	}
	
	private void addJNetworkTab(Network n)
	{
		JNetwork temp = new JNetwork(n);
		
		networkArray.add(temp);
		networkProcesTabs.addTab(temp.toString(), temp);
		networkProcesTabs.validate();
	}
	
	public void runSimulation(Object data)
	{
		boolean flag = false;
		
		for(JNetwork network : this.networkArray)
		{
			network.fire(myControlWidget.getSelectedOdor());
				
			if (outputGraph == null || flag == false)
			{
				outputGraph = new XYInputsvOuputByThetaGraph(network);
				flag = true;
			}
			else
			{
				outputGraph.addXYSeries(network);
			}		
		
			outputGraph.setVisible(true);
			
			this.validate();
			this.repaint();
			
			network.resetGranules();
		}
	}
	
	public void initializeGraphics()
	{
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setSize(new Dimension(X_OFFSET+JNetwork.COL_START_X+JNetwork.COL_OFFSET*networkArray.get(0).getNumCols(), 
				Y_OFFSET+JNetwork.COL_START_Y+JNetwork.GRANULE_RADIUS*JNetwork.getGranulesPerColumn(networkArray.get(0))));
		this.setTitle("Main Simulation");
	}
	
	public JNetwork getJNetwork() 
	{
		return null;
	}
}
